app.areaMgr = {
	_areas : [],
	_areaTypes : [],

	_freeze : false,
	_currentArea : '',
	_currentLocation : null,
	_currentActiveLocations : [],
	_stage : null,
	
	init : function() {
		var that = this;
		
		this._stage = $('<div>').attr('id', 'areaStage').appendTo('#body');
		
		$(document).keypress(function(event) {
			var directions = {
				37 : 'left',
				38 : 'up',
				39 : 'right',
				40 : 'down'
			};
			
			if (that._freeze) {
				return false;
			}
			
			that.move(directions[event.keyCode]);
			
			return false;
		});
		
		this.initAreas();
	},
	
	initAreas : function() {
		var that = this;
		
		$.each(this._areas, function(id, area) {
			var areaLayer = $('<div>').addClass('area').attr('id', 'area-' + id);
			
			this._currentArea = id;
			
			for (var y = 0; y < area.map.length; y++) {
				for (var x = 0; x < area.map[y].length; x++) {
					var location = $('<div>').addClass('location').attr('id', 'area-' + id + '-location-' + x + '-' + y);
					location.addClass(area.map[y][x]);
					
					if (x == 0) {
						location.addClass('first');
					}
					
					if (x == area.map[y].length - 1) {
						location.addClass('last');
					}
					
					location.appendTo(areaLayer);
				}
			}
			
			for (var way in area.ways) {
				var selector = area.ways[way].coords.replace(/(\d+):(\d+)/, '#area-' + id + '-location-$1-$2');
				$(selector, areaLayer).addClass(way == 'start' ? 'start' : 'exit');
			}
			
			areaLayer.appendTo(that._stage);
		});
	},
	
	displayArea : function(id, location) {
		var area, coords;
		
		area = this._areas[id];
		location = area.ways[location] || area.ways.start;
		coords = location.coords.split(':');
		
		$('#area-' + this._currentArea).hide();
		this._currentArea = id;
		
		this.setCurrentLocation(coords[0], coords[1]);
		
		$('#area-' + id).show();
	},
	
	setCurrentLocation : function(posX, posY) {
		var sight = app.player.sight;
		var px = 0, py = 0, cell, type;
		
		if (this._currentLocation) {
			$(this._lastLocation).removeClass('current');
		}
		
		if (this._currentActiveLocations.length > 0) {
			$.each(this._currentActiveLocations, function(index, item) {
				$(item).removeClass('active');
			});
			
			this._currentActiveLocations = [];
		}
		
		this._currentLocation = $('#area-' + this._currentArea + '-location-' + posX + '-' + posY).addClass('current');
		
		for (var x = -sight; x < sight + 1; x++) {
			for (var y = -sight; y < sight + 1; y++) {
				if ((Math.abs(x) + Math.abs(y)) <= sight) {
					px = +posX + x;
					py = +posY + y;
					cell = $('#area-' + this._currentArea + '-location-' + px + '-' + py);
					
					if (cell.size()) {
						type = app._types[app._areas[app._currentArea].map[py][px]];
						
						if (!type.obstacle) {
							this._currentActiveLocations.push(cell.addClass('active'));
						}
					}
				}
			}
		}
	},
	
	move : function(direction) {
		var steps = {
			'left' : [-1, 0],
			'up' : [0, -1],
			'right' : [1, 0],
			'down' : [0, 1]
		};
		var location = this._currentLocation;
		var coords = location.attr('id').replace(/^.*location\-/, '').split('-');
		var step = steps[direction];
		var newCoords = [+coords[0] + step[0], +coords[1] + step[1]];
		var newLocationObject = $('#area-' + this._currentArea + '-location-' + newCoords[0] + '-' + newCoords[1]);
		var area = this._areas[this._currentArea];
		var newLocation = area && area.map && area.map[newCoords[1]] && this._types[area.map[newCoords[1]][newCoords[0]]];
		
		if (!newLocationObject.size() || !newLocation || newLocation.obstacle) {
			return false;
		}
		
		this.setCurrentLocation(newCoords[0], newCoords[1]);
		
		for (var id in area.ways) {
			if ((newCoords[0] + ':' + newCoords[1]) == area.ways[id].coords && area.ways[id].area) {
				this.displayArea(area.ways[id].area, area.ways[id].location);
			}
		}
		
		this.checkEnemies(area, newLocation);
		
		$(app).trigger('locationchange', area, newLocation);
	},
	
	checkEnemies : function(area, location) {
		var fightChance, enemyType;
		
		if (!area.enemies) {
			return false;
		}
		
		fightChance = Math.floor(Math.random() * 100);
		
		if (area.enemies) {
			for (var i in area.enemies) {
				if (fightChance > i) {
					continue;
				}

				enemyType = area.enemies[i];
				
				if (fightChance <= i) {
					break;
				}
			}
		}
		
		if (enemyType) {
			$(app).trigger('fightinit', enemyType);
		}
	}
};